package com.feng.study.demo.juc;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.IntStream;

/**
 * CompletableFuture
 * JDK1.8引入的
 * CompletableFuture相当于一个Task编排工具
 * 异步回调
 * <p>
 * 学习：https://blog.csdn.net/sermonlizhi/article/details/123356877
 */
public class CompletableFutureDemo {

    static ThreadPoolExecutor executor = new ThreadPoolExecutor(2,2,0L,TimeUnit.SECONDS, new LinkedBlockingDeque());

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> future = new CompletableFuture<>();
        CompletableFuture.runAsync(() -> {
        });

//        test01();
//        test02();
        test03();
    }

    private static void test03() throws ExecutionException, InterruptedException {
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "-----");
        },executor);
        System.out.println(future.get());

        CompletableFuture<String> supplyAsync = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "supplyAsync");
//            int a=1/0;
            return "supplyAsync";
        }, executor).whenComplete((v,e)->{
            if (e==null){
                String str=v+"---whenComplete";
                System.out.println(str);
            }else{
                System.out.println("异常报错了");
            }
        }).exceptionally((e)->{
            e.printStackTrace();
            System.out.println("异常情况："+e.getCause()+"\t"+e.getMessage());
            return null;
        });

        System.out.println(supplyAsync.get());
    }

    private static void test02() {
        CompletableFuture<String> task1 = CompletableFuture.supplyAsync(() ->{
            //int kk = 10/0;
            return Thread.currentThread().getName() + "：小郭";
        },executor).thenApply(s -> {
            return s + "拿茶叶";
        }).thenApply(a ->{
            return a + ",泡茶去";
        }).handle((result, ex) ->{
            if (ex != null){
                System.out.println(ex.getMessage());
            }
            return result;
        }).whenComplete((r, ex) ->{
            System.out.println(r);
        });
        task1.join();
    }

    private static void test01() {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (new Random().nextInt(10) % 2 == 0) {
                int i = 12 / 0;
            }
            System.out.println("执行结束！");
            return "test";
        });
        // 任务完成或异常方法完成时执行该方法
        // 如果出现了异常,任务结果为null
        future.whenComplete(new BiConsumer<String, Throwable>() {
            @Override
            public void accept(String t, Throwable action) {
                System.out.println(t + " 执行完成！");
            }
        });
        // 出现异常时先执行该方法
        future.exceptionally(new Function<Throwable, String>() {
            @Override
            public String apply(Throwable t) {
                System.out.println("执行失败：" + t.getMessage());
                return "异常xxxx";
            }
        });

        try {
            future.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

    }
}
